import { http } from "@/http/request";
import { ReqCreateOrder, ReqPrepay, ReqSign } from "./types";
// import { BASE_API } from "@/utils/service/http/config.service";

// 查询字典
const dict = (data: string) => {
	return http.get(`/sys/dict/wxGetDictItems/${data}`);
};

// 创建订单
const createOrder = (data: ReqCreateOrder) => {
	return http.post(`/jinzhuangli-pm-service/wm/order/deposit/createOrder?projectId=${data.projectId}`);
};

// 预支付下单
const cashierPrepay = (data: ReqPrepay) => {
	return http.post(`/jinzhuangli-pm-service/cashier/prepay`, { ...data });
};

// 获取sign签名
const wechatJsapiSign = (data: ReqSign) => {
	return http.post(`/jinzhuangli-pm-service/wechat/jsapi/sign`, { ...data });
};
// oss直传签名信息
export function getOssSign() {
	return http.get(`/system/oss/policy`);
}
/**
 * 上传图片
 * @param {string} filePath
 * @return
 */
export function uploadScaleImg(filePath: string) {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: "https://api-test.jinzhuangli.com/system/sys/common/upload/pic/scale/1",
			filePath,
			formData: {
				biz: "temp"
			},
			name: "file",
			success: (response) => {
				if (response.statusCode == 200) {
					let data = JSON.parse(response.data);
					resolve(data);
				}
			},
			fail: (error) => {
				reject(error);
				console.error(error);
			}
		});
	});
}

// 视频上传OSS直传
export function uploadDirectOss(filePath: string) {
	return new Promise(async (resolve, reject) => {
		try {
			const { data } = await getOssSign();
			if (!data)
				throw {
					errMsg: "获取Oss请求签名信息失败"
				};
			const { accessid, callback, dir, host, policy, signature, expire } = data;
			let params = {
				key: dir + filePath.slice(-15),
				policy,
				OSSAccessKeyId: accessid,
				success_action_status: "200",
				callback,
				signature
			};
			uni.uploadFile({
				url: host,
				filePath,
				formData: {
					biz: "temp",
					...params
				},
				name: "file",
				success: (response) => {
					if (response.statusCode == 200) {
						let data = JSON.parse(response.data);
						resolve(data);
					} else {
						reject({ errMsg: "Oss直传失败" });
					}
				},
				fail: (error) => {
					reject(error);
					console.error(error);
				}
			});
		} catch (e) {
			//TODO handle the exception
			reject(e);
			console.error(e);
		}
	});
}
export { dict, createOrder, cashierPrepay, wechatJsapiSign };
